<?php
class CommentController extends DefaultController{
	public function __construct(){
		$departmentList = DepartmentModel::getList();
		$tree = array();
		mkTree($departmentList, $tree);
		$this->assign('departmentList', $tree);
		parent::__construct();
	}
	
	public function _default(){
		$id = request('department_id');
		$department = array();
		if (!$id){
			$ip = get_client_ip();
			
			$ipInfo = IpBusiness::calcIp($ip);
			if (isset($ipInfo['department_id']) && $ipInfo['department_id']){
				$department = DepartmentModel::get($ipInfo['department_id']);
			}
		}else{
			$department = DepartmentModel::get($id);
		}
		if (!$department){
			$this->_404();
		}
		$where = "`department_id`='{$id}' and `status`='replied'";
		$page = $this->page(CommentModel::getCount($where));
		$commentList = CommentModel::getList($where, $this->start, $this->perpage);
		$this->assign('page', $page);
		$this->assign('commentList', $commentList);
		$this->display();
	}
	
	public function create(){
		$id = request('department_id');
		$department = array();
		if (!$id){
			$ip = get_client_ip();
			
			$ipInfo = IpBusiness::calcIp($ip);
			if (isset($ipInfo['department_id']) && $ipInfo['department_id']){
				$department = DepartmentModel::get($ipInfo['department_id']);
			}
		}else{
			$department = DepartmentModel::get($id);
		}
		
		if (!$department){
			$this->_404('您没有评论的权限');
		}
		$this->assign('department', $department);
		
		$this->display();
	}
}